package org.kandroid.memtracer;

import android.app.Activity;
import android.app.Application;
import android.app.Instrumentation;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class MemoryInstrumentation extends Instrumentation {
	private static final String TAG = "MemoryInstrumentation";
	
    private static final String ARGUMENT_INSTRUMENT_CLASS = "class";

	private MemoryTracer mMemoryTracer;

	private String mMainActivityName;
	
	public MemoryInstrumentation() {
		
	}
	
	@Override
	public void onCreate(Bundle arguments) {
		Log.d(TAG, "onCreate()");
		mMainActivityName = arguments.getString(ARGUMENT_INSTRUMENT_CLASS);
		mMemoryTracer = createMemoryTracer();
		mMemoryTracer.startTracing(getTargetContext().getPackageName());
		start();
	}
	
	protected MemoryTracer createMemoryTracer() {
		return new MemoryTracer(new MemoryTraceCsvWriter());
	}
	
	@Override
	public void onStart() {
		Log.d(TAG, "onStart()");
		String mainActivityName = getMainActivityName();
		if (mainActivityName != null && mainActivityName.length() > 0) {
			launchMainActivity(getTargetContext().getPackageName(), getMainActivityName());
		}
	}

	protected String getMainActivityName() {
		return mMainActivityName;
	}
	
	@SuppressWarnings("unchecked")
	private final void launchMainActivity(String pkg, String actvityCls) {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.setClassName(pkg, actvityCls);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		this.startActivitySync(intent);
		this.waitForIdleSync();
	}
    
	@Override
	public void onDestroy() {
		Log.d(TAG, "onDestroy()");
		mMemoryTracer.stopTracing();
		super.onDestroy();
	}

	@Override
	public void callActivityOnCreate(Activity activity, Bundle icicle) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnCreate");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnCreate(activity, icicle);
	}

	@Override
	public void callActivityOnDestroy(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnDestroy");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnDestroy(activity);
	}

	@Override
	public void callActivityOnNewIntent(Activity activity, Intent intent) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnNewIntent");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnNewIntent(activity, intent);
	}

	@Override
	public void callActivityOnPause(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnPause");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnPause(activity);
	}

	@Override
	public void callActivityOnPostCreate(Activity activity, Bundle icicle) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnPostCreate");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnPostCreate(activity, icicle);
	}

	@Override
	public void callActivityOnRestart(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnRestart");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnRestart(activity);
	}

	@Override
	public void callActivityOnRestoreInstanceState(Activity activity,
			Bundle savedInstanceState) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnRestoreInstanceState");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnRestoreInstanceState(activity, savedInstanceState);
	}

	@Override
	public void callActivityOnResume(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnResume");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnResume(activity);
	}

	@Override
	public void callActivityOnSaveInstanceState(Activity activity,
			Bundle outState) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnSaveInstanceState");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnSaveInstanceState(activity, outState);
	}

	@Override
	public void callActivityOnStart(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnStart");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnStart(activity);
	}

	@Override
	public void callActivityOnStop(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnStop");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnStop(activity);
	}

	@Override
	public void callActivityOnUserLeaving(Activity activity) {
		Bundle snapshot = mMemoryTracer.addSnapshot(activity.getLocalClassName()+"-OnUserLeaving");
//		Log.d(TAG, snapshot.toString());
		super.callActivityOnUserLeaving(activity);
	}

	@Override
	public void callApplicationOnCreate(Application app) {
		Bundle snapshot = mMemoryTracer.addSnapshot(app.getPackageName()+"-OnAppCreate");
//		Log.d(TAG, snapshot.toString());
		super.callApplicationOnCreate(app);
	}
	
}
